
import api from './api.js';
import vues from '../main.js'


class SingleLogin {
  constructor() {
    this.instance = null
    this.openId = null
    this._resolve = null
    this._check = false
    this.token = null
    this.callbacks = []
    this.approval_remark = null
    this.NeedOpenid = null
  }

  static getInstance() {
    if (!this.instance) {
      this.instance = new SingleLogin()
    }
    return this.instance
  }
  async _check_session() {
    return new Promise((resolve, reject) => {
      uni.checkSession({
        success: function(res) {　　　　　　
          resolve(true)　　　　
        },
        fail: function(res) {　　　　
          reject()　　　　
        }
      })
    }).catch(err => { })
  }
  async _wxLogin() {
    return new Promise((resolve, reject) => {
      uni.login({
        success: res => {
          resolve(res.code)
        },
        fail: err => {
          reject()
        },
        complete: (msg) => {
          console.log(msg, 'msg')
        }
      })
    }).catch(err => { })
  }
  async code_totoken() {
    let newCode = await this._wxLogin();
    return new Promise(async(resolve, reject) => {
      let res = await this._request(api.GetTestToken, {
        Js_code: newCode,
        ProgramCate: "Wechat"
      });
     
      if (res.Success) {
        this.NeedOpenid = res.Data.IsNeedSecondOpenid;
        console.log(vues)
		vues.$store.dispatch('setOpenid',res.Data.Openid);
		vues.$store.dispatch('setToken',res.Data.Token);
        // 存token
        uni.setStorageSync('Openid', res.Data.Openid);
        uni.setStorageSync('Token', res.Data.Token);
        this.token = res.Data.Token;
        resolve(res.Data.Token)
      } else {
        reject()
      }
    }).catch(err => { })
  }
  getToken() {
    return new Promise(async(resolve, reject) => {
      this.callbacks.push(resolve)
      let session = await this._check_session();
      if (!session) {
        let token = await this.code_totoken();
        resolve(SingleLogin.getInstance().token);
      } else {
        if (SingleLogin.getInstance().token) {
          resolve(SingleLogin.getInstance().token)
        } else {
          let localToken = uni.getStorageSync('Token');
          if (localToken) {
            // let res = await this.checkLogin(localToken);
            // if (!res) {
            //   let token = await this.code_totoken();
            //   resolve(SingleLogin.getInstance().token);
            // } else {
              this.token = localToken;
              resolve(SingleLogin.getInstance().token);
            // }

          } else {
            let token = await this.code_totoken();
            resolve(SingleLogin.getInstance().token);
          }
          resolve(SingleLogin.getInstance().token)
        }
      }


    }).catch(err => {})
  }
  checkLogin(token) {

    return new Promise(async(resolve, reject) => {

      let res = await this._request(api.GetWebConfig);

      if (res.Success) {
        this.token = token;
        this.NeedOpenid = res.Data.IsNeedOpenId;
        resolve(token)
      } else {
        reject()
      }
    }).catch(err => {})
  }

  async _request(url, data) {
    return new Promise((resolve, reject) => {
   
      uni.request({
        url: url,
        data: data,
        method: 'POST',
        header: { 'content-type': 'application/x-www-form-urlencoded', 'Authorization': uni.getStorageSync('Token')||'' },
        success: (res => {

          resolve(res.data);
        }),
        fail: (res => {
          console.log(res)
          reject()
        })
      })
    }).catch(err => {})
  }


  clearToken() {
    SingleLogin.getInstance().token = null
    uni.removeStorageSync('Token')
  }
}
export default SingleLogin